package cn.iocoder.an.module.business.controller.admin.feedback;

import cn.iocoder.an.module.business.dal.dataobject.equipmentmanage.EquipmentManageDO;
import cn.iocoder.an.module.business.dal.dataobject.equipmnetkeepwait.EquipmnetKeepWaitDO;
import cn.iocoder.an.module.business.utils.WeComMessageUtil;
import cn.iocoder.an.module.system.api.notify.dto.NotifySendSingleToUserReqDTO;
import cn.iocoder.an.module.system.dal.dataobject.dict.DictDataDO;
import cn.iocoder.an.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.an.module.system.service.dict.DictDataService;
import cn.iocoder.an.module.system.service.user.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.feedback.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.feedback.FeedbackDO;
import cn.iocoder.an.module.business.service.feedback.FeedbackService;

@Tag(name = "管理后台 - 快捷反馈")
@RestController
@RequestMapping("/business/feedback")
@Validated
public class FeedbackController {

    @Resource
    private FeedbackService feedbackService;

    @PostMapping("/create")
    @Operation(summary = "创建快捷反馈")
    @PreAuthorize("@ss.hasPermission('business:feedback:create')")
    public CommonResult<Long> createFeedback(@Valid @RequestBody FeedbackSaveReqVO createReqVO) {
        return success(feedbackService.createFeedback(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新快捷反馈")
    @PreAuthorize("@ss.hasPermission('business:feedback:update')")
    public CommonResult<Boolean> updateFeedback(@Valid @RequestBody FeedbackSaveReqVO updateReqVO) {
        feedbackService.updateFeedback(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除快捷反馈")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:feedback:delete')")
    public CommonResult<Boolean> deleteFeedback(@RequestParam("id") Long id) {
        feedbackService.deleteFeedback(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除快捷反馈")
                @PreAuthorize("@ss.hasPermission('business:feedback:delete')")
    public CommonResult<Boolean> deleteFeedbackList(@RequestParam("ids") List<Long> ids) {
        feedbackService.deleteFeedbackListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得快捷反馈")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:feedback:query')")
    public CommonResult<FeedbackRespVO> getFeedback(@RequestParam("id") Long id) {
        FeedbackDO feedback = feedbackService.getFeedback(id);
        return success(BeanUtils.toBean(feedback, FeedbackRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得快捷反馈分页")
    @PreAuthorize("@ss.hasPermission('business:feedback:query')")
    public CommonResult<PageResult<FeedbackRespVO>> getFeedbackPage(@Valid FeedbackPageReqVO pageReqVO) {
        PageResult<FeedbackDO> pageResult = feedbackService.getFeedbackPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FeedbackRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出快捷反馈 Excel")
    @PreAuthorize("@ss.hasPermission('business:feedback:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFeedbackExcel(@Valid FeedbackPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FeedbackDO> list = feedbackService.getFeedbackPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "快捷反馈.xls", "数据", FeedbackRespVO.class,
                        BeanUtils.toBean(list, FeedbackRespVO.class));
    }

}